local addon, db = ...

local frame = CreateFrame"Frame"
frame:SetScript("OnEvent", function (self, event, ...)
	local handlers = self[event]
	if handlers then
		for handler in next, handlers do
			if not handler(self, event, ...) then
				handlers[handler] = nil
			end
		end
		if not next(handlers) then
			self:UnregisterEvent(event)
			self[event] = nil
		end
	end
end)

function db.Register(handler, ...)
	for i = 1, select("#", ...) do
		local event = select(i, ...)
		local handlers = frame[event]
		if not handlers then
			handlers = {}
			frame[event] = handlers
			frame:RegisterEvent(event)
		end
		handlers[handler] = true
	end
end

function db.Unregister(handler, ...)
	for i = 1, select("#", ...) do
		local event = select(i, ...)
		local handlers = assert(frame[event])
		handlers[handler] = nil
		if not next(handlers) then
			frame:UnregisterEvent(event)
			frame[event] = nil
		end
	end
end

function db.OnInit(handler)
	if IsLoggedIn() then
		handler()
	else
		db.Register(handler, "PLAYER_LOGIN")
	end
end

db.Frame = frame

-- remove guild channel autojoin
if SetGuildRecruitmentMode then
	SetGuildRecruitmentMode(0)
end

SetCVar("CombatHealing", "1")
JerryDB = {}


-- item
local slots = {
	HeadSlot = 1,
	NeckSlot = 0.5,
	ShoulderSlot = 0.75,
	-- ShirtSlot = 0,
	ChestSlot = 1,
	WaistSlot = 0.75,
	LegsSlot = 1,
	FeetSlot = 0.75,
	WristSlot = 0.5,
	HandsSlot = 0.75,
	Finger0Slot = 0.5,
	Finger1Slot = 0.5,
	Trinket0Slot = 0.75,
	Trinket1Slot = 0.75,
	BackSlot = 0.5,
	-- MainHandSlot = 1,
	-- SecondaryHandSlot = 0.75,
	-- RangedSlot = 1,
	-- TabardSlot = 0,
}

local qualityRatio = {
	[ITEM_QUALITY_UNCOMMON] = 0.5,
	[ITEM_QUALITY_RARE] = 0.625,
	[ITEM_QUALITY_EPIC] = 0.7692,
	[5] = 1, -- legendary
	[7] = 0.5,
}

local function get_item_info(unit, slot)
	local index = GetInventorySlotInfo(slot)
	local itemLink = GetInventoryItemLink(unit, index)
	if not itemLink then return end
	local _, _, q, l, _, _, _, _, s = GetItemInfo(itemLink)
	if not q or not l then return end
	return l * qualityRatio[q], s
end

local function calc_weapon_values(unit)
	local i1 = GetInventorySlotInfo"MainHandSlot"
	local i2 = GetInventorySlotInfo"SecondaryHandSlot"
	local i3 = GetInventorySlotInfo"RangedSlot"
	
	local v1, s1 = get_item_info(unit, "MainHandSlot")
	local v2, s2 = get_item_info(unit, "MainHandSlot")
	local v3, s3 = get_item_info(unit, "RangedSlot")
	
	local _, class = UnitClass(unit)
	if class == "WARRIOR" and s1 == "INVTYPE_2HWEAPON" and v2 then
		return v1 * 0.55 + v2 * 0.45 + v3 * 0.3, 1.3
	elseif class == "HUNTER" then
		return v1 * 0.5 + v2 * 0.5 + v3 * 1.0, 2.0
	elseif s1 == "INVTYPE_2HWEAPON" then
		return v1 * 1.0 + v3 * 0.3, 1.3
	else
		return v1 * 0.55 + v2 * 0.45 + v3 * 0.3, 1.3
	end
		
end

local function calc(unit)
	local sum_value, sum_ratio = 0, 0
	for slot, ratio in next, slots do
		local level = get_item_info(unit, slot)
		if level then
			sum_value = sum_value + level * ratio
		end
		sum_ratio = sum_ratio + ratio
	end
	local w_value, w_ratio = calc_weapon_values(unit)
	sum_value = sum_value + w_value
	sum_ratio = sum_ratio + w_ratio
	local result = (sum_value / sum_ratio) / qualityRatio[ITEM_QUALITY_EPIC]
	print(string.format("avgLevel for %s is %.1f (%.1f / %.1f)", UnitName(unit), result, sum_value, sum_ratio))
end

SlashCmdList.JERRY_ILEVEL = function (input)
	if input and UnitExists(input) then calc (input) end
end
SLASH_JERRY_ILEVEL1="/ilevel"

local current_set = nil
db.Register(function ()
	if current_set then
		UseEquipmentSet(current_set)
		current_set = nil
	end
	return true
end, "ACTIVE_TALENT_GROUP_CHANGED")
SlashCmdList.JERRY_EQUIP = function(input)
	local talentGroup = 3 - GetActiveTalentGroup()
	local set1, set2 = (" "):split(input)
	current_set = (talentGroup == 1 and set1 or set2)
	SetActiveTalentGroup(talentGroup)
end
SLASH_JERRY_EQUIP1="/jswitch"
